﻿Imports Microsoft.VisualBasic

Public Class clsConvertBase
    Const base10 As Int32 = 10
    Dim cHexa As String() = New String() {"A", "B", "C", "D", "E", "F"}
    Dim iHexaNumeric As Int32() = New Int32() {10, 11, 12, 13, 14, 15}
    Dim iHexaIndices As Int32() = New Int32() {0, 1, 2, 3, 4, 5}
    Const asciiDiff As Int32 = 48

    Function DenaryToBaseX(ByVal denary As Long, ByVal base As Integer) As String
        Dim remainder As Integer = 0
        Dim c As String = "0123456789ABCDEF"  'The digits the function uses for bases larger than 10
        Try
            DenaryToBaseX = ""  'The result

            If base < 2 OrElse base > 16 Then  'If the base is unsuitable
                Return "Base cannot be less than 2 or greater than 16!"
            End If

            If denary = 0 Then  '0 is the same in all bases!
                Return "0"
            End If
            'This is part which does all of the work
            Do Until denary = 0
                remainder = denary Mod base
                denary = denary \ base
                DenaryToBaseX = c.Substring(remainder, 1) & DenaryToBaseX
            Loop
        Catch ex As Exception
            Throw ex
        End Try


        Return DenaryToBaseX 'Return the result
    End Function

    Public Function convert16To10(ByVal strBase16 As String) As String
        Dim str As String = ""
        Dim lsum As Long = 0
        Dim strText As String = ""

        Try
            If Not strBase16 = "" Then
                Dim intCountLength As Integer = strBase16.Length

                For i As Integer = 0 To intCountLength - 1
                    str = Mid(strBase16, intCountLength - i, 1)
                    If IsNumeric(str) Then
                        lsum += Convert.ToDouble(str) * 16 ^ i
                    Else
                        lsum += iHexaNumeric(Array.BinarySearch(cHexa, str)) * 16 ^ i
                    End If

                Next

            End If
        Catch ex As Exception
            Throw ex
        End Try




        Return lsum
    End Function


End Class
